<template>
  <div class="scenic-village">
    <!-- <div class="scenic-village-title"></div> -->
    <div class="accordion-container">
      <div 
        v-for="(item, index) in accordionItems" 
        :key="index" 
        class="accordion-item"
      >
        <div class="accordion-header" :class="{ 'active': index === openIndex }"  @click="toggleAccordion(index)">
          <div class="accordion-title">{{ item.title }}</div>
        </div>
        <div  class="accordion-content" :style="{ width: index == openIndex ? '748px' : '0' }">
          <img :src="item.picUrl||mergeImage" alt="" class="accordion-image">
          <div class="accordion-label">
            <div class="accordion-more" @click="handleVillageClick(item)">【查看详情】</div>
            <div class="accordion-label-title">{{item.title}}</div>
            <div class="accordion-label-text scrollbar">{{item.description}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue';
import mergeImage from '@/assets/images/mergeImage.png'
import { useRouter  } from 'vue-router'

const router = useRouter()

const props = defineProps({
  data: {
    type: Array,
    default: []
  },
})

const openIndex = ref(0);
const accordionItems = ref([]);

// 初始化数据
onMounted(() => {
  accordionItems.value = props.data.slice(0, 4)
})

// 监听 data 变化并更新 accordionItems
watch(() => props.data, (newData) => {
  if (newData && newData.length > 0) {
    accordionItems.value = newData.slice(0, 4)
    // 重置打开的索引为第一个
    openIndex.value = 0
  }
}, { 
  deep: true,  // 深度监听数组变化
  immediate: true // 立即执行，组件初始化时也会执行
})

const toggleAccordion = (index) => {
  openIndex.value = index
};

// 在现有代码中添加这个方法
const handleVillageClick = (item) => {

  // 跳转到村社区详情页，假设路由为 '/village'，参数为村社区ID
  router.push({ 
    path: '/village', 
    query: { id: item.orgId } 
  });
};
</script>

<style lang="scss" scoped>
.scenic-village {
  height: 959px;
  background: url('@/assets/images/scenic-village.png') no-repeat center center;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: -110px;
  .scenic-village-title{
    margin-top: 183px;
    width: 985px;
    height: 83px;
    background-image: url('@/assets/images/scenic-title.png');
  }
  .accordion-container {
    margin-top: 341px;
    display: flex;
    .accordion-item {
      border: 1px solid #ccc;
      margin-bottom: 10px;
      overflow: hidden;
      display: flex;

      .accordion-header {
        background-color: #f1f1f1;
        font-size: 18px;
        cursor: pointer;
        user-select: none;
        width: 131px;
        height: 495px;
        background: url("@/assets/images/col-bg1.png");
        display: flex;
        justify-content: flex-end;
        &.active{
          background: url("@/assets/images/col-bg2.png");
        }
        .accordion-title{
          font-weight: 600;
          font-size: 28px;
          color: #FFFFFF;
          margin-top:154px;
          height: 280px;
          line-height: 40px;
          padding: 0 26px;
          letter-spacing: 5px;
          writing-mode: vertical-rl;       /* 竖排，从右到左 */
        }
      }

      .accordion-content {
        background-color: #fff;
        overflow: hidden;
        height: 495px;
        transition: width 0.5s ease-out;
        position: relative;
        &:hover .accordion-label {
          top: 0; // 滑入到顶部
          pointer-events: auto; // 鼠标移入后允许交互
        }
        .accordion-image{
          width: 748px;
          height: 495px;
        }
        .accordion-label{
          position: absolute;
          top: 100%;
          // top: 0;
          left: 0;
          width: 748px;
          height: 495px;
          z-index: 2;
          overflow: hidden;
          background-color: rgba($color: #000000, $alpha: 0.55);
          transition: top 0.3s ease-in-out;
          transition-delay:0.2s;
          pointer-events: none; // 避免阻挡点击事件
          .accordion-more{
            position: absolute;
            right: 40px;
            top: 30px;
            color: #fff;
            font-size: 14px;
            cursor: pointer;
          }
          .accordion-label-title{
            font-weight: 500;
            font-size: 30px;
            color: #FFFFFF;
            margin: 90px 40px 0;
          }
          .accordion-label-text{
            height: 200px;
            font-weight: 400;
            font-size: 18px;
            color: #FFFFFF;
            line-height: 34px;
            margin: 30px 40px 0;
            overflow: auto;
          }
        }
      }
    }
  }
}
</style>